//
//  HTHomeTableViewController.swift
//  weibo
//
//  Created by JonsonLiu on 2017/2/22.
//  Copyright © 2017年 JonsonLiu. All rights reserved.
//

import UIKit

class HTHomeTableViewController: HTBasController {
    
//    var userLogin : Bool = true
//    
//    override func loadView() {
//        if userLogin == true {
//            super.loadView()
//        }else{
//            
//            let v = UIView()
//            v.backgroundColor = UIColor.orange
//            view = v
//            
//            
//        }
//    }
    
    var stateViewModel : StatesListViewModel = StatesListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        stateViewModel.loadData(isPullUP: true) { (isSuccess) in
            if isSuccess {
                self.tableView.reloadData()
            }else{
                print("列表错误")
            }
        }
    }
    
//    func loadData(){
//    
//        let url = "https://api.weibo.com/2/statuses/home_timeline.json"
//        let param = [
//            "access_token" : "2.00VbzG3CbZxgeBafd89365faJgnK_B"
//        ]
//        NetworkTools.shardTools.request(method: .Get, urlString: url, parameters: param as AnyObject?) { (responseObject, error) in
//            
//            if error != nil {
//            
//                print(error!)
//                return
//            }
//            
//            guard let resDict = responseObject as? [String : AnyObject]else{
//                
//                return
//            }
//            
//            guard let dicts = resDict["statuses"] as? [[String : AnyObject]] else {
//                return
//            }
//            
//            
//            print("这里是最后的数组 \(dicts)")
//            var stateArray = [HTStates]()
//            
//            for value in dicts {
//                let state = HTStates(dict : value)
//                
//                stateArray.append(state)
//            }
//            
//            self.state = stateArray
//            
//            self.setupUI()
//            
//            self.tableView.reloadData()
//        }
//    }
    
    
    func setupUI(){
        
        tableView.register(HTStatesCell.self, forCellReuseIdentifier: "ss")
        
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 250
        tableView.separatorStyle = .none
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

//    override func numberOfSections(in tableView: UITableView) -> Int {
//        // #warning Incomplete implementation, return the number of sections
//        return 1
//    }

    
    

}

extension HTHomeTableViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return stateViewModel.state?.count ?? 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let  cell = tableView.dequeueReusableCell(withIdentifier: "ss", for: indexPath) as! HTStatesCell
        
//        cell.textLabel?.text = stateViewModel.state?[indexPath.row].text
        cell.statesViewModel = stateViewModel.state?[indexPath.row]
        
        
        
        
        
        
        return cell
    }

    
}
